namespace OFX.Aggregator.WpfApplication
{
	using System;
	using System.Collections;

	using Castle.Windsor;
	using Castle.Windsor.Configuration.Interpreters;

	using Infrastructure;

	public class WindsorContainerAbstraction : IIocContainerAbstraction
	{
		private readonly IWindsorContainer container;

		public WindsorContainerAbstraction()
		{
//#if DEBUG
//            const string configFile = @"config\IoC-debug.config";
//#else
			const string configFile = @"config\IoC.config";
//#endif

			this.container = new WindsorContainer(new XmlInterpreter(configFile));
		}

		private WindsorContainerAbstraction(IWindsorContainer container) { this.container = container; }

		public IWindsorContainer Instance
		{
			get { return this.container; }
		}

		#region Implementation of IIoCContainerAbstraction

		public void AddComponentInstance<T>(Type serviceType, object instance) { Instance.Kernel.AddComponentInstance<T>(serviceType, instance); }

		public void ReleaseComponent(object instance) { Instance.Kernel.ReleaseComponent(instance); }

		public object Resolve(Type service, string key, IDictionary parameters)
		{
			if (key == null && parameters == null)
				return this.container.Resolve(service);
			if (parameters == null)
				return this.container.Resolve(key, service);
			if (key == null)
				return this.container.Resolve(service, parameters);

			return this.container.Resolve(key, service, parameters);
		}

		#endregion
	}
}